package com.heima.wemedia.service.impl;

import com.heima.common.constants.message.ArticleVisitStreamMess;
import com.heima.common.exception.CustException;
import com.heima.feigns.ArticleFeign;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmFansStatisticsDto;
import com.heima.model.wemedia.vos.WmFansListVo;
import com.heima.model.wemedia.vos.WmFansStatisticsVo;
import com.heima.utils.common.DateUtils;
import com.heima.wemedia.service.WmFansService;
import javassist.runtime.DotClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;

/**
 * @author lo
 * @date 2021/10/11
 */
@Service
@Slf4j
public class WmFansServiceImpl implements WmFansService {
    @Autowired
    ArticleFeign articleFeign;


    /**
     * 查询全部文章行为数据,以及文章数量
     * 根据时间查询文章列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findArticle(WmFansStatisticsDto dto) {
        // List<WmFansStatisticsVo> resultList = new ArrayList<>();
        //1.校验参数
        if (dto == null || StringUtils.isBlank(dto.getType())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Integer type = Integer.valueOf(dto.getType());

        String[] time = dto.getTime();
        //2. 通过time来查询
        if (type == -1) {

            ResponseResult<List<ApArticle>> allArticle = articleFeign.findAllArticle(time[0], time[1]);
            if (!allArticle.checkCode() || allArticle.getData() == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }
            List<WmFansStatisticsVo> wmFansStatisticsVos = statisticsVo(allArticle);
            return ResponseResult.okResult(wmFansStatisticsVos);

        }
        //3.按照前端毫秒值查询
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //开始时间
        String stime = simpleDateFormat.format(dto.getStime());
        //截止时间
        String etime = simpleDateFormat.format(dto.getEtime());
        //4.远程调用查询文章
        ResponseResult<List<ApArticle>> allArticle = articleFeign.findAllArticle(stime, etime);
        if (!allArticle.checkCode() || allArticle.getData() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //封装返回
        List<WmFansStatisticsVo> wmFansStatisticsVos = statisticsVo(allArticle);

        // resultList.add(wmFansStatisticsVo);
        return ResponseResult.okResult(wmFansStatisticsVos);
    }

    /**
     * 聚合结果
     * @param allArticle
     * @return
     */
    // public WmFansStatisticsVo statisticsVo(ResponseResult<List<ApArticle>> allArticle){
    //     List<ApArticle> apArticleList = allArticle.getData();
    //     //计算各个行为数据的和
    //     Optional<WmFansStatisticsVo> reduceResult = apArticleList.stream()
    //             .map(apArticle -> {
    //                 WmFansStatisticsVo wmFansStatisticsVo = new WmFansStatisticsVo();
    //                 BeanUtils.copyProperties(apArticle, wmFansStatisticsVo);
    //                 wmFansStatisticsVo.setRead_count(apArticle.getViews());
    //                 return wmFansStatisticsVo;
    //             })
    //             // 累加：聚合计算
    //             .reduce(new BinaryOperator<WmFansStatisticsVo>() {
    //                 @Override
    //                 public WmFansStatisticsVo apply(WmFansStatisticsVo w1, WmFansStatisticsVo w2) {
    //                     //点赞
    //                     w1.setLikes(w1.getLikes() + w2.getLikes());
    //                     //阅读
    //                     w1.setRead_count(w1.getRead_count() + w2.getRead_count());
    //                     //收藏
    //                     w1.setCollection(w1.getCollection() + w2.getCollection());
    //                     //评论
    //                     w1.setComment(w1.getComment() + w2.getComment());
    //                     // //关注
    //                     // w1.setFollow(w1.getFollow() + w2.getFollow());
    //
    //                     return w1;
    //                 }
    //             });
    //     // 汇总结果
    //     if (!reduceResult.isPresent()) {
    //         log.error("聚合失败 WmFansServiceImpl 第109行");
    //         CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"数据为空");
    //     }
    //     WmFansStatisticsVo wmFansStatisticsVo = reduceResult.get();
    //     wmFansStatisticsVo.setArticle(apArticleList.size());
    //     log.info("根据日期查询文章聚合结果  ===>{}" , wmFansStatisticsVo);
    //     wmFansStatisticsVo.setCreatedTime(new Date());
    //     return wmFansStatisticsVo;
    // }

    public List<WmFansStatisticsVo> statisticsVo(ResponseResult<List<ApArticle>> allArticle) {
        List<ApArticle> apArticleList = allArticle.getData();
        //计算各个行为数据的和
        List<WmFansStatisticsVo> collect = apArticleList.stream()
                .map(apArticle -> {
                    WmFansStatisticsVo wmFansStatisticsVo = new WmFansStatisticsVo();
                    BeanUtils.copyProperties(apArticle, wmFansStatisticsVo);
                    wmFansStatisticsVo.setRead_count(apArticle.getViews());
                    wmFansStatisticsVo.setId(1);
                    wmFansStatisticsVo.setArticle(1);
                    wmFansStatisticsVo.setCreatedTime(apArticle.getCreatedTime());
                    //假数据
                    wmFansStatisticsVo.setFollow(30);
                    wmFansStatisticsVo.setForward(100);
                    wmFansStatisticsVo.setUserId(10);

                    return wmFansStatisticsVo;
                }).collect(Collectors.toList());

        return  collect;
    }
}
